//
//  Category.swift
//  jmwebseoApp1803
//
//  Created by YQ05 on 2018/3/30.
//  Copyright © 2018年 YQ05. All rights reserved.
//

import Foundation
//import ObjectMapper
import Moya

struct CategoryIndexResponse{
    var status : String!
    var count : Int!
    var categories : [Category]!
    
    
//    init?(map: Map) {
//        
//    }
//    mutating func mapping(map: Map) {
//        status <- map["status"]
//        count <- map["count"]
//        categories <- map["categories"]
//    }
}

struct Category{
    var id : Int!
    var title : String!
    var count : Int!
    
    
//    init?(map: Map) {
//        
//    }
//    mutating func mapping(map: Map) {
//        id <- map["id"]
//        title <- map["title"]
//        count <- map["post_count"]
//    }
}

extension Category{
    
    //获取新闻分类
   static func request(completion:@escaping ([Category]?) -> Void){
        
        let provider = MoyaProvider<NetworkService>()
        provider.request(.category) { result in
            switch result {
            case let .success(moyaResponse):
                break
//                let json = try! moyaResponse.mapJSON() as! [String:Any]
                
//                if let jsonResponse = CategoryIndexResponse(JSON: json){
//                    //print(jsonResponse.count,jsonResponse.status,jsonResponse.categories)
//                    completion(jsonResponse.categories)
//                }
            // do something with the response data or statusCode
            case  .failure:
                print("网络错误1")
                 completion(nil)
                // this means there was a network failure - either the request
                // wasn't sent (connectivity), or no response was received (server
                // timed out).  If the server responds with a 4xx or 5xx error, that
                // will be sent as a ".success"-ful response.
            }
        }
        
    }
}




